var toggleValue = 0;
$(document).ready(function() {

    //## ----------------------------------
    //## Checks to see if theme-cookie is set,
    //## if not, load default theme.
    //## ----------------------------------
    if($.cookie('theme') != null){
        var theme = $.cookie('theme');
        //alert(theme)
        switch(theme){
            case 'midgard':
                loadTheme(theme);
                break;
            case 'midgreen':
                loadTheme(theme);
                break;
            default:
                loadTheme('midgard');
                break;
        }
    };

    now.messageAll = function (opts) {
        now.broadcastNotification(opts);
    };

    now.gritt = function(opts) {
        $.gritter.add({
            title: opts.title,
            text: opts.text,
            position: "top-right",
            image: opts.image || './images/sintef_logo.png',
            target: opts.target || '#content',
            sticky: true
        });
    };

    now.notify = function(text) {
        $.notification(text, {
            position: "bottom-right"
        })
    };




    // var canvas = $('#canvas');
    // var context = canvas.getContext("2d");
    // context.moveTo(100, 150);
    // context.lineTo(450, 50);
    // context.stroke();

    // $('ul#display input').button();

    $( "button" ).button();
    $( "a", ".nav" ).click(function() { return false; });
    $('nav li a').click(function() {
        var action = $(this).data("action");
// #MERGE
        // function loadContent() {
        //     $('#content').load(toLoad,'',showNewContent)
        // }

        // function showNewContent() {
        //     $('#content').show('normal',hideLoader);
        // }

        // function hideLoader() {
        //     $('#load').fadeOut('normal');
        // }
        return false;
    });

        $( "#dialog-modal" ).dialog({
            // height: 140, 
            dialogClass: 'alert',
            modal: true,
            type: 'alert',
            show: 'slide',
            autoOpen: false,
            buttons: {
                "Create": function() { 
                    var title = $( "#dialog-modal input.title" ).val();
                    var description = $( "#dialog-modal input.description" ).val();
                    now.createIdea({
                        title: title,
                        description: description
                    });
                    $(this).dialog("close");
                }
            }
        });


    // $('#fullscreen').click(function() {
        // $('section#content').toggleClass("fullscreen", $(this).val()==="on");
    // });

    //## ----------------------------------
    //## Used for managing and loading content from
    //## direct links to webpages ("http://localhost/*").
    //## I.e: "/#newIdea", "/#test.html"
    //## ----------------------------------
    var hash = window.location.hash.substr(1);
    var href = $('nav a').each(function(){
        var href = $(this).attr('href');
        if(hash==href){
            var toLoad = hash+' #newcontent';
            $('#content').load(toLoad)
        }
    });

    $('.nav li a').click(function() {
        //alert("Panda BEER is coming to town");
        var toLoad = $(this).attr('href')+' #newcontent';
        $('#content').hide('fast',loadContent);
        //## ----------------------------------
        //## Loads content to container #content
        //##
        //## ----------------------------------
    });


    $.piroBox_ext({
        piro_speed :700,
        bg_alpha : 0.5,
        piro_scroll : true,
        piro_drag :false,
        piro_nav_pos: 'bottom'
    });

    now.ready(function() {
        // now.hello("world", function(phrase) {alert(phrase);});
    });

    //## ----------------------------------
    //## Manages functions for draggable
    //## menu & handle
    //## ----------------------------------
    $(".module").draggable({
        // handle: "#handle",
        // axis: "y",
        // containment: "#sidebar",
        scroll: false
    });

    $('div.task').click(function() {
        taskId = $(this).data('taskid');
        alert("TaskID: "+taskId);
    });

    //scrollpane parts
    var scrollPane = $( ".scroll-pane" );
    var scrollContent = $( ".scroll-content" );

    //build slider
    $( "#timeslider" ).slider({
        slide: function( event, ui ) {
            if ( scrollContent.width() > scrollPane.width() ) {
                scrollContent.css( "margin-left", Math.round(
                    ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
                ) + "px" );
            } else {
                scrollContent.css( "margin-left", 0 );
            }
        }
    });

    $('#notification-counter').click(function() {
        $('#notifications-menu').toggle();
    });

    $('#togglemenu').click(function(){
        $('.nav').toggle("slide",{ direction:"left"}, 600);
        if (toggleValue == 0){
            $(this).attr("src","/images/close.png");
            toggleValue= 1;
        } else {
            $(this).attr("src","/images/slidein.png");
            toggleValue= 0;
        }

        return false;
    });
    //## ----------------------------------
    //## Changes theme based on current
    //## selection value from id='themechanger',
    //## and changes logo in the same manner
    //## ----------------------------------
    $('#themechanger').change(function(){
        var selectedtheme = $('#themechanger').val();
        loadTheme(selectedtheme);
        //$('link#theme').attr('href','/stylesheets/themes/'+ selectedtheme +".css");
        //('img#mainlogo').attr('src','/images/logo_'+selectedtheme+'.png');
        $.post('/saveCookie', {themechanger:selectedtheme});
        return false;
    });

    //## ----------------------------------
    //## Loads themename received from parameter
    //## ----------------------------------
    function loadTheme(theme){
        $('link#theme').attr('href','/stylesheets/themes/'+theme+".css");
        $('img#mainlogo').attr('src','/images/logo_'+theme+'.png');
        $("option").attr('selected','');
        $("option[value="+theme+"]").attr('selected','selected');
    }


}); // END document.ready()


/*
SAFETOWN #MERGE

    //## ----------------------------------
    //## Menu show/hide function
    //## ----------------------------------
    $('#togglemenu').click(function(){
        $('.nav').toggle("slide",{ direction:"left"}, 600);
        if (toggleValue == 0){
            $(this).attr("src","/images/arrowright_s.png");
            $("#content").animate({"left":"40px"},700);
            toggleValue= 1;
        } else {
            $(this).attr("src","/images/arrowleft_s.png");
            $("#content").css({"left":""},700);
            toggleValue= 0;
        }
 
        return false;
        });


//## ----------------------------------
    //## Menubutton functions
    //## ----------------------------------
    $('#ideaButton').click(function() {
        $('#newIdea').toggle();
        return false;
    });
 
    $('#proposalButton').click(function() {
        $('#newProposal').toggle();
        return false;
    });
 
    $('#taskButton').click(function() {
        $('#newTask').toggle();
        return false;
    });
 
    $('#newTaskB').click(function() {
        alert("Howdy");
        $('#dialogform').dialog();
    });
*/